package com.ruoyi.match.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.match.mapper.LeaderTeamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.match.mapper.MemberTeamMapper;
import com.ruoyi.match.domain.MemberTeam;
import com.ruoyi.match.service.IMemberTeamService;

/**
 * 队员队伍Service业务层处理
 * 
 * @author zhangrui
 * @date 2025-03-26
 */
@Service
public class MemberTeamServiceImpl implements IMemberTeamService 
{
    @Autowired
    private MemberTeamMapper memberTeamMapper;

    @Autowired
    private LeaderTeamMapper leaderTeamMapper;

    /**
     * 查询队员队伍
     * 
     * @param userId 队员队伍主键
     * @return 队员队伍
     */
    @Override
    public MemberTeam selectMemberTeamByUserId(Long userId)
    {
        return memberTeamMapper.selectMemberTeamByUserId(userId);
    }

    /**
     * 查询队员队伍列表
     * 
     * @param memberTeam 队员队伍
     * @return 队员队伍
     */
    @Override
    public List<MemberTeam> selectMemberTeamList(MemberTeam memberTeam)
    {
        return memberTeamMapper.selectMemberTeamList(memberTeam);
    }

    /**
     * 新增队员队伍
     * 
     * @param memberTeam 队员队伍
     * @return 结果
     */
    @Override
    public int insertMemberTeam(MemberTeam memberTeam)
    {
        // 查询用户是否已在该队伍
        Long userId = memberTeam.getUserId();
        System.out.println("userId:"+userId);
        System.out.println("TeamId:"+memberTeam.getTeamId());

//        int count = memberTeamMapper.checkIfMemberExists(userId, memberTeam.getTeamId());
//        int count2 = leaderTeamMapper.checkIfLeaderExists(userId,memberTeam.getTeamId());
//        if (count > 0 || count2 > 0) {
//            System.out.println("你已加入该队伍，请勿重复加入！");
//            throw new ServiceException("你已加入该队伍，请勿重复加入！");
//        }
        return memberTeamMapper.insertMemberTeam(memberTeam);
    }

    /**
     * 修改队员队伍
     * 
     * @param memberTeam 队员队伍
     * @return 结果
     */
    @Override
    public int updateMemberTeam(MemberTeam memberTeam)
    {
        return memberTeamMapper.updateMemberTeam(memberTeam);
    }

    /**
     * 批量删除队员队伍
     * 
     * @param userIds 需要删除的队员队伍主键
     * @return 结果
     */
    @Override
    public int deleteMemberTeamByUserIds(Long[] userIds)
    {
        return memberTeamMapper.deleteMemberTeamByUserIds(userIds);
    }

    /**
     * 删除队员队伍信息
     * 
     * @param userId 队员队伍主键
     * @return 结果
     */
    @Override
    public int deleteMemberTeamByUserId(Long userId)
    {
        return memberTeamMapper.deleteMemberTeamByUserId(userId);
    }


    //    用户自己退出队伍
    @Override
    public int leaveTeam(Long userId, Long teamId) {


        return memberTeamMapper.leaveTeam(userId,teamId);
    }

    @Override
    public int removeMember(MemberTeam memberTeam) {
        return memberTeamMapper.removeMember(memberTeam);
    }
}
